// pages/index/product/product.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: '',
    history:'',
    hidden:true,
    keyword:'',
    imageurl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.setNavigationBarTitle({
      title: '商品价格',
    });
    that.setData({
      imageurl: app.globalData.baseImageUrl
    })
    app.post('/shop/getGoodsPrice').then(res => {
      that.setData({
        data: res,
        history: wx.getStorageSync("history") ? wx.getStorageSync("history"):[]
      })
    });
  },
  //搜索函数
  search:function(keyword){
    app.post()
  },
  //输入框获得焦点时候
  focus:function(){
    let that = this;
    that.setData({
      hidden:false
    })
  },
  clearS:function(){
    this.setData({
      history: [],
    })
    wx.setStorageSync("history",[]);
  },
  //点击历史的时候重新排序历史，并搜索
  selhis:function(e){
    let that = this;
    let s = e.currentTarget.dataset.his;
    that.inputSearch(e,s);
  },
  inputSearch:function(e,s){
    let that = this;
    wx.showLoading({
      title: '搜索中',
    })
    let keyword = s?s:e.detail.value;
    let history = wx.getStorageSync("history") ? wx.getStorageSync("history"):[];
    if (keyword) history = [keyword].concat(history);
    if (history.length > 10) history = history.slice(0, 12);
    wx.setStorageSync("history", history);
    app.post('/shop/searchGoodsPrice', { keyword: keyword}).then(res=>{
      wx.hideLoading()
      that.setData({
        data: res,
        keyword: keyword,
        history: history,
        hidden:true
      })
    })
  }
})